//
//  WTPrivateLetterView.m
//  66TV
//
//  Created by frank on 16/7/29.
//  Copyright © 2016年 shifeng. All rights reserved.
//

#import "WTPrivateLetterView.h"
#import "Masonry.h"
#import "WTPrivateLetterLeftCell.h"
#import "WTPrivateLetterRightCell.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "UIColor+WTExtend.h"
#import "WTBaseMessageModel.h"
#import "WTLiveSDK.h"


@interface WTPrivateLetterView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView*  privateLetterTableView;
@property (nonatomic, strong) NSArray<WTBaseMessageModel*>*      dataSource;
@end


@implementation WTPrivateLetterView
- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
     
        [self setup];
    }
    return self;
}

#pragma mark - 
- (void)setup{
    
    self.privateLetterTableView = ({
        
        UITableView* view = [[UITableView alloc] init];
        view.backgroundColor = [UIColor hexChangeFloat:@"f0f0f0"];
        [view registerNib:[UINib nibWithNibName:@"WTPrivateLetterLeftCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([WTPrivateLetterLeftCell class])];
        [view registerNib:[UINib nibWithNibName:@"WTPrivateLetterRightCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([WTPrivateLetterRightCell class])];
        view.separatorStyle = UITableViewCellSeparatorStyleNone;
        view.dataSource = self;
        view.delegate = self;
        [self addSubview:view];
        view;
    });
    
    [self layout];
}

#pragma mark - 
- (void)layout{
    
    [self.privateLetterTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}


#pragma mark - 
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    WTBaseMessageModel* model = self.dataSource[indexPath.row];
    if([model.chat_message_net_ease_id isEqualToString:WTConfig.net_accid]){
        WTPrivateLetterRightCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WTPrivateLetterRightCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        WTBaseMessageModel* model = self.dataSource[indexPath.row];
        cell.messageModel = model;
        return cell;
    }else {
        WTPrivateLetterLeftCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WTPrivateLetterLeftCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        WTBaseMessageModel* model = self.dataSource[indexPath.row];
        cell.messageModel = model;
        return cell;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    WTBaseMessageModel* model = self.dataSource[indexPath.row];
    if([model.chat_message_net_ease_id isEqualToString:WTConfig.net_accid]){
        return [tableView fd_heightForCellWithIdentifier:NSStringFromClass([WTPrivateLetterRightCell class]) configuration:^(WTPrivateLetterRightCell* cell) {
            WTBaseMessageModel* model = self.dataSource[indexPath.row];
            cell.messageModel = model;
        }];
    }else {
        return [tableView fd_heightForCellWithIdentifier:NSStringFromClass([WTPrivateLetterLeftCell class]) configuration:^(WTPrivateLetterLeftCell* cell) {
            WTBaseMessageModel* model = self.dataSource[indexPath.row];
            cell.messageModel = model;
        }];
    }
}



#pragma mark public methods
- (void)insertMessages:(NSArray<WTBaseMessageModel *> *)messages{
    
//    NSMutableArray* tmpMessages = [NSMutableArray arrayWithCapacity:0];
//    for (WTBaseMessageModel* model in messages) {
//        if([model.chat_message_net_ease_id isEqualToString:self.private_chat_net_ease]){
//            [tmpMessages addObject:model];
//        }
//        
//    }
    if(!self.dataSource){
        self.dataSource = messages;
        [self.privateLetterTableView reloadData];
    }else {
        NSMutableArray* datas = [[NSMutableArray alloc] initWithArray:self.dataSource];
        [datas addObjectsFromArray:messages];
        self.dataSource = datas;
        [self.privateLetterTableView reloadData];
    }
    [self.privateLetterTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.dataSource count] - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

- (void)insertLocalMessages:(NSArray<NIMMessage *> *)messages{
    
    NSMutableArray* insertMessages = [NSMutableArray arrayWithCapacity:0];
    [messages enumerateObjectsUsingBlock:^(NIMMessage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        NSDictionary*  messageExt = [obj.remoteExt mj_JSONObject];
        NSString* icon = messageExt[@"avatar"];
        if(!icon){
            icon = messageExt[@"wx_avatar"];
        }
        NSString* name = messageExt[@"name"];
        NSInteger level = [messageExt[@"level"] integerValue];
        
        WTBaseMessageModel* model = [[WTBaseMessageModel alloc] init];
        model.sessionType = NIMSessionTypeP2P;
        model.chat_message_text = obj.text;
        model.chat_message_avtar_icon = icon;
        model.chat_message_avtar_name = name;
        model.chat_room_message_level = level;
        model.chat_message_net_ease_id = messageExt[@"net_ease_id"];
        [insertMessages addObject:model];
    }];
    
    [self insertMessages:insertMessages];
}
@end
